const BaseIpcHandler = require('../BaseIpcHandler');
const fs = require('fs');

class DeleteDirectoryHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log('>> DeleteDirectoryHandler params = ', params);
        const { dir } = params;

        // 业务逻辑
        let result = null;
        if (fs.existsSync(dir)) {
            fs.rmSync(dir, { recursive: true, force: true });
            result = { code: 0, msg: `Directory '${dir}' deleted successfully.` };
        } else {
            result = { code: -1, msg: `Directory '${dir}' does not exist.` };
        }

        // 返回结果
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = DeleteDirectoryHandler;
